#include "VertexBuffer.h"

VertexBuffer::VertexBuffer(Graphics & gfx, const std::vector<GeometryGenerator::Vertex>& vertices)
{
	D3D11_BUFFER_DESC bd = {};
	bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
	bd.Usage = D3D11_USAGE_DEFAULT;
	bd.CPUAccessFlags = 0;
	bd.MiscFlags = 0;
	bd.ByteWidth = (UINT)(sizeof(GeometryGenerator::Vertex) * vertices.size());
	bd.StructureByteStride = sizeof(GeometryGenerator::Vertex);

	D3D11_SUBRESOURCE_DATA sd = {};
	sd.pSysMem = vertices.data();
	HR(GetDevice(gfx)->CreateBuffer(&bd, &sd, &vertexBuffer));
}

void VertexBuffer::Bind(Graphics & gfx)
{
	const UINT stride = sizeof(GeometryGenerator::Vertex);
	const UINT offset = 0;
	GetContext(gfx)->IASetVertexBuffers(0, 1, vertexBuffer.GetAddressOf(), &stride, &offset);
}

std::shared_ptr<VertexBuffer> VertexBuffer::Create(Graphics & gfx, const std::vector<GeometryGenerator::Vertex>& vertices)
{
	return std::make_shared<VertexBuffer>(gfx,vertices);
}
